<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.thymeleaf.org/thymeleaf-extras-shiro">
<head>
    <meta charset="UTF-8">
    <title>供应商管理</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <link rel="icon" href="/favicon.ico">
    <link rel="stylesheet" href="/resources/layui/css/layui.css" media="all"/>
    <link rel="stylesheet" href="/resources/css/public.css" media="all"/>
</head>
<body class="childrenBody">
<!-- 查询条件开始-->
<form class="layui-form">
    <blockquote class="layui-elem-quote quoteBox">
        <form class="layui-form">
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">供应商名称</label>
                    <div class="layui-input-inline">
                        <input type="text" name="name" id="name" class="layui-input searchVal" placeholder="请输入供应商名称"/>
                    </div>
                    <button type="button" class="layui-btn" lay-submit lay-filter="doSearch" shiro:hasPermission="provider:view"><span
                            class="layui-icon layui-icon-search"></span>查询
                    </button>
                    <button type="reset" class="layui-btn layui-btn-warm"><span
                            class="layui-icon layui-icon-refresh-1"></span>重置
                    </button>
                </div>
            </div>
        </form>
    </blockquote>
</form>
<!-- 查询条件结束-->


<!-- 数据表格开始 -->
<div>
    <table class="layui-hide" id="supplierTable" lay-filter="supplierTable"></table>
    <!-- 头部工具栏 -->
    <div id="supplierToolBar" style="display: none;">
        <button type="button" lay-event="add" class="layui-btn layui-btn-sm" shiro:hasPermission="provider:create"><span
                class="layui-icon layui-icon-add-1"></span>添加供应商
        </button>
        <button type="button" lay-event="batchDelete" class="layui-btn layui-btn-sm layui-btn-danger" shiro:hasPermission="provider:delete"><span
                class="layui-icon layui-icon-delete"></span>批量删除
        </button>
    </div>

    <!-- 行工具栏 -->
    <div id="supplierRowBar" style="display: none;">
        <button type="button" lay-event="update" class="layui-btn layui-btn-sm" shiro:hasPermission="provider:update"><span
                class="layui-icon layui-icon-edit"></span>更新
        </button>
        <button type="button" lay-event="delete" class="layui-btn layui-btn-sm layui-btn-danger" shiro:hasPermission="provider:delete"><span
                class="layui-icon layui-icon-delete"></span>删除
        </button>
    </div>
</div>
<!-- 数据表格结束 -->

<!-- 添加和修改的弹出层开始 -->
<div style="display: none;padding: 5px" id="addOrUpdateDiv">
    <form class="layui-form" style="width:80%;" id="dataFrm" lay-filter="dataFrm">
        <div class="layui-form-item layui-row layui-col-xs12">
            <label class="layui-form-label">供应商名称</label>
            <div class="layui-input-block">
                <!-- 修改时生效 -->
                <input type="hidden" name="id">
                <input type="text" class="layui-input" name="name" lay-verify="required" placeholder="请输入供应商名称">
            </div>
        </div>
        <div class="layui-form-item layui-row layui-col-xs12">
            <label class="layui-form-label">联系人</label>
            <div class="layui-input-block">
                <input type="text" class="layui-input" name="contact" lay-verify="required" placeholder="请输入联系人">
            </div>
        </div>
        <div class="layui-form-item layui-row layui-col-xs12">
            <label class="layui-form-label">联系电话</label>
            <div class="layui-input-block">
                <input type="text" class="layui-input" name="number" lay-verify="required" placeholder="请输入联系电话">
            </div>
        </div>
        <div class="layui-form-item layui-row layui-col-xs12">
            <label class="layui-form-label">联系地址</label>
            <div class="layui-input-block">
                <input type="text" class="layui-input" name="address" lay-verify="required" placeholder="请输入联系地址">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">供应商备注</label>
            <div class="layui-input-block">
                <textarea class="layui-textarea" name="remarks" id="content"></textarea>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">是否删除</label>
                <div class="layui-input-block">
                    <input type="radio" name="inDel" value="1" title="是" checked="">
                    <input type="radio" name="inDel" value="0" title="否">
                </div>
            </div>
        </div>
        <div class="layui-form-item layui-row layui-col-xs12">
            <div class="layui-input-block" style="text-align: center;">
                <button type="button" class="layui-btn" lay-submit lay-filter="doSubmit" id="doSubmit"><span
                        class="layui-icon layui-icon-add-1"></span>提交
                </button>
                <button type="reset" class="layui-btn layui-btn-warm"><span
                        class="layui-icon layui-icon-refresh-1"></span>重置
                </button>
            </div>
        </div>
    </form>
</div>
<!-- 添加和修改的弹出层结束 -->

<!-- 查看弹出层的开始 -->
<div style="display: none;padding: 5px" id="showNoticeDiv">
    <h2 id="show_title" align="center"></h2>
    <div style="text-align: right;margin: 10px">
        <span>发布人:<span id="show_opername"></span></span>
        <span style="padding-left: 20px">发布时间:<span id="show_createtime"></span></span>
    </div>
    <hr>
    <div id="show_content" style="margin: 10px;padding: 10px;line-height: 25px;"></div>

</div>
<!-- 查看弹出层的结束-->


<script type="text/javascript" src="/resources/layui/layui.js"></script>
<script type="text/javascript">
    layui.use(['jquery', 'form', 'table', 'layer', 'laydate', 'layedit'], function () {
        var $ = layui.jquery;
        var form = layui.form;
        var table = layui.table;
        var layer = layui.layer;
        var laydate = layui.laydate;
        var layedit = layui.layedit;


        //渲染数据表格
        var tableIns = table.render({
            elem: '#supplierTable'//绑定表格组件的id元素
            , url: '/bus/supplier/supplierlist'//数据发送的请求路径
            , toolbar: '#supplierToolBar' //开启头部工具栏，并为其绑定左侧模板
            , title: '供应商数据表'
            , height: 'full-180'
            , page: true
            , cols: [
                [
                {type: 'checkbox', fixed: 'left'}
                , {field: 'id', title: 'ID', align: 'center', width: 80}
                , {field: 'name', title: '供应商', align: 'center'}
                , {field: 'contact', title: '联系人', align: 'center'}
                , {field: 'number', title: '联系电话', align: 'center'}
                , {field: 'address', title: '联系地址', align: 'center'}
                , {field: 'right', title: '操作', align: 'center', toolbar: "#supplierRowBar", width: 300}
                ]
            ]
            //回调函数
            , done: function (res, curr, count) {
                // 判断当前页码是否大于1
                if (curr > 1 && res.data.length == 0) {
                    //当前页码-1
                    var pageValue = curr - 1;
                    tableIns.reload({
                        page: {curr: pageValue}// 修改页码
                    })
                }
            }
        });
        //监听模糊查询按钮点击事件
        form.on("submit(doSearch)", function (data) {
            tableIns.reload({
                where: data.field,
                page: {
                    curr: 1
                }
            });
        });
        //头部工具栏监听事件
        table.on("toolbar(supplierTable)", function (obj) {
            switch (obj.event) {
                case 'add':
                    openAddSupplierWindow();
                    break;
                case 'batchDelete':
                    batchDelete();//批量删除
                    break;
            }
        });
        //弹出层索引，提交路径
        var mainIndex, url;

        //打开添加供应商窗口
        function openAddSupplierWindow() {
            mainIndex = layer.open({
                type: 1,
                title: "添加供应商",
                area: ["800px", "500px"],
                content: $("#addOrUpdateDiv"),//引用的内容窗口
                success: function () {
                    //清空表单数据
                    $("#dataFrm")[0].reset();
                    //添加的提交请求
                    url = "/bus/supplier/addSupplier";
                }
            });
        }

        //监听表单的提交事件
        form.on("submit(doSubmit)", function (data) {
            $.post(url, data.field, function (result) {
                if (result.success) {
                    //刷新数据表格
                    tableIns.reload();
                }
                //显示提示信息
                layer.msg(result.message);
                //关闭模态框
                layer.close(mainIndex);
            }, "json");
            return false;
        });
        //监听行工具栏事件
        table.on("tool(supplierTable)", function (obj) {
            switch (obj.event) {
                case 'update':
                    openUpdateSupplierWindow(obj.data);
                    break;
                case 'delete':
                    deleteSupplier(obj.data);
                    break;
            }
        });

        //修改供应商
        function openUpdateSupplierWindow(data) {
            mainIndex = layer.open({
                type: 1,
                title: "更新供应商",
                area: ["800px", "500px"],
                content: $("#addOrUpdateDiv"),//引用的内容窗口
                success: function () {
                    //表单数据回显
                    form.val("dataFrm", data);
                    //添加的提交请求
                    url = "/bus/supplier/updateSupplier";
                }
            });
        }

        /**
         * 删除供应商
         * @param data
         */
        function deleteSupplier(data) {
            layer.confirm("确定要删除这条数据吗?", {"icon": 3, "title": "提示"}, function (index) {
                $.post("/bus/supplier/deleteById", {"id": data.id}, function (result) {
                    if (result.success) {
                        //刷新表格
                        tableIns.reload();
                    }
                    //显示提示信息
                    layer.msg(result.message);
                }, "json");
                //关闭弹框
                layer.close(index);
            })
        }

        /**
         * 批量删除
         */
        function batchDelete() {
            //获取选中行
            var checkStatus = table.checkStatus('supplierTable'); //supplierTable 即为lay-filter的属性值
            //获取选中的数量
            var length = checkStatus.data.length;
            //判断长度是否大于0
            if (length > 0) {
                //提示是否删除
                layer.confirm("确定要删除这" + length + "条数据吗?", {icon: 3, title: '提示'}, function (index) {
                    var checkData = checkStatus.data;//获取每一行的数据
                    //声明数组，保存选中行的id
                    var idArr = [];
                    for (var i = 0; i < length; i++) {
                        //获取选中行的id
                        idArr.push(checkData[i].id);
                    }
                    //id数组
                    var ids = idArr.join(",");
                    //发送删除的请求
                    $.post("/bus/supplier/delete", {"ids": ids}, function (result) {
                        //判断是否成功
                        if (result.success) {
                            //删除成功后刷新数据表格
                            tableIns.reload();
                        }
                        layer.msg(result.message);//弹出提示信息
                    }, "json");
                    //关闭提示框
                    layer.close(index);
                });
            } else {
                //没有选中行，提示请选择要删除的行！
                //layer.alert('请选择要删除的行！',{icon: 0});
                layer.msg("请选择要删除的行！");
            }
        }

    });
</script>
</body>
</html>